package com.yifi.common.plugins.IoTDB;

import com.jfinal.plugin.IPlugin;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.session.util.Version;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * @author 姚善利
 * @classname com.yifi.common.plugins
 * @description
 * @date 2022/10/14 14:45
 */
public class IoTDBPlugin implements IPlugin {

    private static final Logger log = Logger.getLogger(IoTDBPlugin.class);

    protected String host;
    protected int port;
    protected List<String> nodeUrls;
    protected int fetchSize=-1;
    protected String username;
    protected String password;
    protected int thriftDefaultBufferSize;
    protected int thriftMaxFrameSize;
    protected boolean enableCacheLeader;
    protected Version version;

    protected Session session;

    public IoTDBPlugin() {

    }

    public IoTDBPlugin(String host,int port) {
        this.host=host;
        this.port=port;
    }

    public IoTDBPlugin(List<String> nodeUrls) {
        this.nodeUrls=nodeUrls;
    }

    public IoTDBPlugin(String host,int port,String username,String password,
                       int fetchSize,int thriftDefaultBufferSize,
                       int thriftMaxFrameSize,boolean enableCacheLeader,Version version) {
        this.host=host;
        this.port=port;
        this.username=username;
        this.password=password;
        this.fetchSize=fetchSize;
        this.thriftDefaultBufferSize=thriftDefaultBufferSize;
        this.thriftMaxFrameSize=thriftMaxFrameSize;
        this.enableCacheLeader=enableCacheLeader;
        this.version=version;
    }

    @Override
    public boolean start() {
        if(this.host!=null)
        {
            // 指定一个可连接节点
            session =
                    new Session.Builder()
                            .host(this.host)
                            .port(this.port)
                            .build();
        }
        else if(this.nodeUrls!=null)
        {
            // 指定多个可连接节点
            session =
                    new Session.Builder()
                            .nodeUrls(this.nodeUrls)
                            .build();
        }
        else if(this.fetchSize!=-1)
        {
            // 其他配置项
            session =
                    new Session.Builder()
                            .fetchSize(this.fetchSize)
                            .username(this.username)
                            .password(this.password)
                            .thriftDefaultBufferSize(this.thriftDefaultBufferSize)
                            .thriftMaxFrameSize(this.thriftMaxFrameSize)
                            .enableCacheLeader(this.enableCacheLeader)
                            .version(this.version)
                            .build();
        }
        else
        {
            // 全部使用默认配置
            session = new Session.Builder().build();
        }

        try {
            session.open();
            log.info("iotdb连接成功~");
            // 设置时区
            session.setTimeZone("+08:00");
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
        } catch (StatementExecutionException e) {
            e.printStackTrace();
        }
        IoTDBUtils.AddSession(session);
        return true;
    }

    @Override
    public boolean stop() {
        try {
            this.session.close();
            IoTDBUtils.close();
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
        }
        return false;
    }
}
